// pages/car/takecar/index.ts
import {saveCar,Car,pageCar,takePageCar} from '../../../api/car'
import { LENGYU_SUCCESS_CODE,PageRequestParam } from '../../../utils/request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageParam:{
      pageIndex:1,
      pageSize:10,
      paramMapObj:{
      }
    },
    searchOver:false,
    refresherTriggered:false,
    carsList:[
    ],
    title:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getPageList(true);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  loadMore() {
    console.log("上拉加载")
    this.getPageList(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onRefresh() {
    console.log("下拉刷新")
    console.log(this.data.refresherTriggered);
    this.setData({
      'pageParam.pageIndex':1
    })
    this.getPageList(true);
    setTimeout(()=>{
      this.setData({
        'refresherTriggered':false
      })
    },1000)
    console.log(this.data.refresherTriggered);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async getPageList(refresh:boolean){
    if(!refresh&& this.data.searchOver){
      console.log("已经查询完成不在继续查询")
      return;
    }
    let resultData :any
      let carListType =  wx.getStorageSync('carListType');
      if(carListType == 'myCar'){
        this.setData({
          'pageParam.paramMapObj.vx_open_id':wx.getStorageSync('openId'),
          'title':"我发起的小破车"
        })
        let result = await pageCar(this.data.pageParam as PageRequestParam);
        resultData = result.data
      }
      else if(carListType == 'takeCar'){
            //do nothing
            this.setData({
              'title':"报名破车列表"
            })
            let result = await pageCar(this.data.pageParam as PageRequestParam);
            resultData = result.data
      }
      else if(carListType == 'myTakeCar'){
        this.setData({
          'pageParam.paramMapObj.vx_open_id':wx.getStorageSync('openId'),
          'title':"我参与的小破车"
        })
        let result = await takePageCar(this.data.pageParam as PageRequestParam);
        resultData = result.data
      }
      
      if(refresh){
        this.setData({
          'carsList':resultData.records
        })
      }
      else{
        this.setData({
          'carsList':this.data.carsList.concat(resultData.records)
        })
      }
      if(resultData.records.size > this.data.pageParam.pageSize){
        this.setData({
          'pageParam.pageIndex':this.data.pageParam.pageIndex+1
        })
      }
      else{
        this.setData({
          'searchOver':true
        })
      }
      console.log(this.data.carsList)
    },
    goToTakeCar(e:any){
      console.log("参数传递" , e)
      wx.navigateTo({
        url: 'addpeople/index',
        success: (res) => {
          // 跳转成功后，触发事件'delivery', 并可携带数据（即第一个参数是事件名，第二个参数是数据包）
          res.eventChannel.emit('deliveryParam', {
              car:  e.currentTarget.dataset.car
            })
          }
      })
    }
})